//
//  RightArrowButton.m
//  Kugou-OC
//
//  Created by BH on 16/5/5.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "RightArrowButton.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>

#define TitleImageW 20

@implementation RightArrowButton

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置文字
        self.titleLabel.textAlignment = NSTextAlignmentRight;
        
        // 高亮时不要让imageView变灰色
        self.adjustsImageWhenHighlighted = NO;
        self.imageView.contentMode = UIViewContentModeCenter;
    }
    return self;
}

-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageY = 0;
    CGFloat imageW = TitleImageW;
    CGFloat imageX = self.width - imageW;
    CGFloat imageH = self.height;
    return CGRectMake(imageX, imageY, imageW, imageH);
}

-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGFloat titleW = self.width - TitleImageW;
    CGFloat titleH = self.height;
    return  CGRectMake(titleX, titleY, titleW, titleH);
}

-(void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    
    // 计算文字的宽度
    CGSize titleSize = [title sizeWithFont:self.titleLabel.font];
    
    // 计算整个按钮的宽度
    self.width = titleSize.width + TitleImageW;
}

@end
